package com.utils.mail;

import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SendMail {
	public static void main(String[] args)throws Exception{
		//发送邮件步骤：
		//3、进入要发邮件的邮箱，打开三方协议POP3/SMPT/IMAP
		//4、设置授权码,该密码为代码中使用的密码，与登录密码不一样
		//0.1 设置基本参数
		Properties props = new Properties();
		//  设置主机
		props.setProperty("mail.host", "smtp.163.com");
		//  确定使用权限验证
		props.setProperty("mail.smtp.auth", "true");
		//0.2 确定账号与密码 
		Authenticator authenticator = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("panpan2016ok@163.com", "qazwsx123");
			}
		};
		//1获得连接
		Session session = Session.getInstance(props, authenticator);
		//2 编写消息
		Message message = new MimeMessage(session);
		// 2.1 发件人
		message.setFrom(new InternetAddress("panpan2016ok@163.com","LPM"));

		// 2.2 收件人 , to:收件人   cc:抄送   bcc:暗送
		 message.setRecipient(RecipientType.TO, new InternetAddress("1398274478@qq.com"));
		
		//设置多个收件人地址
		//message.addRecipient(RecipientType.TO,new InternetAddress("设置收件人的邮箱"));
		 
		// 2.3 主题
		message.setSubject("第一份邮件");
		// 2.4 正文
		String htmlData = "<table width=\"671\" cellspacing=\"0\" cellpadding=\"2\" style=\"width:671px;margin-left:10px; border-collapse:collapse;border:1px solid #CFCFCF;margin-top:10px;font-family:宋体;\"> <tbody><tr> <td colspan=\"4\" style=\"font-size:12px;font-weight:bold;height:25px;line-height:25px;padding-left:10px;background-color:#F0F0F0;\"> <span style=\" color: rgb(255, 0, 0);\">★</span>&nbsp;账户信息 </td> </tr> <tr style=\"text-align: center;font-size:12px;background-color:#F7F7F7;\"> <td style=\"border-right:1px solid #cfcfcf;border-top:1px solid #cfcfcf;\"> <span style=\"font-weight:bold;\">信用额度</span> </td> <td style=\"border-right:1px solid #cfcfcf;border-top:1px solid #cfcfcf;\"> <span style=\"font-weight:bold;\">最低还款额</span> </td> <td style=\"border-right:1px solid #cfcfcf;border-top:1px solid #cfcfcf;\"> <span style=\"font-weight:bold;\">账单日</span> </td> <td style=\"border-right:1px solid #cfcfcf;border-top:1px solid #cfcfcf;\"> <span style=\"font-weight:bold;\">到期还款日</span> </td> </tr> <tr style=\"text-align:center;background-color:#F7F7F7;font-size:12px;\"> <td style=\"border-right:1px solid #cfcfcf;\">Credit Limit</td> <td style=\"border-right:1px solid #cfcfcf;\">Min.Payment</td> <td style=\"border-right:1px solid #cfcfcf;\">Statement Date</td> <td style=\"border-right:1px solid #cfcfcf;\">Payment Due Date</td> </tr> <tr style=\"text-align:center;background-color:#fff;font-size:12px;\"> <td style=\"font-family: Arial,Helvetica,sans-serif;height:22px;line-height:22px;border-right:1px solid #CFCFCF;border-top:1px solid #cfcfcf\">1</td> <td style=\"font-family:Arial,Helvetica,sans-serif;border-right:1px solid #CFCFCF;border-top:1px solid #cfcfcf\">1186.00</td> <td style=\"font-family:Arial,Helvetica,sans-serif;border-right:1px solid #CFCFCF;border-top:1px solid #cfcfcf\">20200710</td> <td style=\"font-family:Arial,Helvetica,sans-serif;border-right:1px solid #CFCFCF;border-top:1px solid #cfcfcf\">20200804</td> </tr> </tbody></table>";
		message.setContent(htmlData, "text/html;charset=UTF-8");
		//3发送消息
		Transport.send(message);
		System.out.println("发送成功");
	}
}
